package handler

import (
	"crypto/sha256"
	"encoding/hex"
	"math/rand"
	"unicode"
)

const saltLength = 16
const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"

// 密码验证函数
func verifyPassword(inputPwd, storedPwd, salt string) bool {
	return hashPassword(inputPwd, salt) == storedPwd
}

// 生成随机盐值(16位)
func generateSalt() string {
	b := make([]byte, saltLength)
	for i := range b {
		b[i] = letters[rand.Intn(len(letters))]
	}
	return string(b)
}

// 密码加盐哈希处理
func hashPassword(password, salt string) string {
	// 使用SHA256哈希算法
	h := sha256.New()
	h.Write([]byte(password + salt))
	return hex.EncodeToString(h.Sum(nil))
}

// 密码强度检查函数
func isStrongPassword(password string) bool {
	hasUpper := false
	hasLower := false
	hasDigit := false

	for _, c := range password {
		switch {
		case unicode.IsUpper(c):
			hasUpper = true
		case unicode.IsLower(c):
			hasLower = true
		case unicode.IsDigit(c):
			hasDigit = true
		}
	}

	return hasUpper && hasLower && hasDigit
}
